package com.quanyan.financialSystem.service.place.impl;

import com.lefu.commons.utils.Page;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.financialSystem.constants.FinancialConstants;
import com.quanyan.financialSystem.entity.vo.req.ReqCashSettleReport;
import com.quanyan.financialSystem.entity.vo.resp.*;
import com.quanyan.financialSystem.mapper.CashSettleReportMapper;
import com.quanyan.financialSystem.service.place.CashSettleReportService;
import org.omg.CORBA.OBJ_ADAPTER;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/10/22.
 */
@Service
public class CashSettleReportServiceImpl implements CashSettleReportService {

    private static final Logger logger = LoggerFactory.getLogger(CashSettleReportServiceImpl.class);
    @Autowired
    private CashSettleReportMapper cashSettleReportMapper;

    @Override
    public APIResponse findAllCashSettleReport(ReqCashSettleReport reqCashSettleReport) {
        logger.info("开吃查询现金结算报表----》{}",reqCashSettleReport.toString());
        Page page = new Page();
        page.setCurrentPage(reqCashSettleReport.getPageNo());
        page.setShowCount(reqCashSettleReport.getPageSize());
        List<RespCashSettleReport> lstResult = cashSettleReportMapper.findAllCashSettleReport(page, reqCashSettleReport);
        if(reqCashSettleReport.getPageNo() > page.getTotalPage()){
            lstResult = new ArrayList<>();
            page.setTotalPage(0);
            page.setTotalResult(0);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("respCashSettleReportTotal",this.getRespCashSettleReportTotal(reqCashSettleReport));
        logger.info("结束查询现金结算报表：{}",lstResult.toString());
        PageObj<List<RespCashSettleReport>> result = PageObj.create(page.getTotalResult(), page.getCurrentPage(), page.getShowCount(), lstResult);
        map.put("pageObj",result);
        return APIResponse.returnSuccess(map);
    }


    public RespCashSettleReportTotal getRespCashSettleReportTotal(ReqCashSettleReport reqCashSettleReport){
        logger.info("开始结算总计信息：{}",reqCashSettleReport.toString());
        RespCashSettleReportTotal respCashSettleReportTotal = new RespCashSettleReportTotal();
        List<CashStatusTotal> lst = cashSettleReportMapper.findCashSettleTotal(reqCashSettleReport);
        for (CashStatusTotal respCashSettleReport : lst){
            if(respCashSettleReport.getCashStatus() == FinancialConstants.APPLY )//申请金额
                respCashSettleReportTotal.setApplyAmount(respCashSettleReport.getDebit());
            else if(respCashSettleReport.getCashStatus() == FinancialConstants.NO_APPLY) //未申请
                respCashSettleReportTotal.setNoApplyAmount(respCashSettleReport.getDebit());
            else if(respCashSettleReport.getCashStatus() == FinancialConstants.Transfer) //转账
                respCashSettleReportTotal.setTransferAmount(respCashSettleReport.getDebit());
            else if(respCashSettleReport.getCashStatus() == FinancialConstants.SETTLE) //转账
                respCashSettleReportTotal.setSettleAmount(respCashSettleReport.getDebit());
            if(respCashSettleReport.getCashStatus() !=0 )
                respCashSettleReportTotal.setOutOfPocketAmount(respCashSettleReportTotal.getOutOfPocketAmount().add(respCashSettleReport.getDebit()));
        }

        logger.info("汇总：{}",respCashSettleReportTotal.toString());
        return respCashSettleReportTotal;
    }



    public APIResponse findAllCashSettleReportPlace(ReqCashSettleReport reqCashSettleReport) {
        logger.info("开吃查询现金结算报表----》{}",reqCashSettleReport.toString());
        Page page = new Page();
        int pageIndex = reqCashSettleReport.getPageNo();
        page.setCurrentPage(pageIndex);
        page.setShowCount(reqCashSettleReport.getPageSize());
        List<RespCashSettleReportPlace> lstResult = cashSettleReportMapper.findAllCashSettleByPlace(page,reqCashSettleReport);
        if(pageIndex > page.getTotalPage()){
            lstResult = new ArrayList<>();
            page.setTotalPage(0);
            page.setTotalResult(0);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("respCashSettleReportTotal",this.getRespCashSettleReportTotal(reqCashSettleReport));

        logger.info("结束查询现金结算报表：{}", lstResult.toString());
        PageObj<List<RespCashSettleReportPlace>> result = PageObj.create(page.getTotalResult(), page.getCurrentPage(), page.getShowCount(), lstResult);
        map.put("pageObj",result);
        return APIResponse.returnSuccess(map);
    }

}
